import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { User } from '@services/data-types/member.type';
import { MemberService } from '@services/member.service';
import { NzMessageService } from 'ng-zorro-antd';
import { timer } from 'rxjs';
import { deleteAllCookies } from '@utils/base64';
import { BatchActionsService } from '@store/batch-actions.service';
import { Router } from '@angular/router';

const NOT_LOGIN_CODE = 301;

@Component({
  selector: 'app-member-card',
  templateUrl: './member-card.component.html',
  styleUrls: ['./member-card.component.less']
})
export class MemberCardComponent implements OnInit {
  point = 0;
  showTip = false;
  tipTitle = '';
  @Input() user: User;
  @Output() openModal = new EventEmitter<void>();
  constructor(
    private router: Router,
    private memberService: MemberService,
    private messageServe: NzMessageService,
    private batchActionsService: BatchActionsService
  ) { }

  ngOnInit() {
  }
  onSigning(): void {
    this.memberService.signIn().subscribe(res => {
      this.messageServe.create('success', '签到成功');
      this.tipTitle =  '积分+' + res.point;
      this.showTip = true;
      timer(1500).subscribe(() => {
        this.showTip = false;
        this.tipTitle = '';
      });
    }, error => {
      if (error.code === NOT_LOGIN_CODE) {
        this.messageServe.create('error', error.msg);
        deleteAllCookies();
        this.batchActionsService.setUserId('');
        localStorage.removeItem('wyyUserId');
        this.router.navigate(['/']).then();
        return;
      }
      this.messageServe.create('error', error.msg || '签到失败');
    });
  }
}
